package com.pcj.blog.web;/**
 * @param
 * @author pcj
 * @date 2021/12/23 11:35
 * @return
 */


import com.pcj.blog.service.BlogService;
import com.pcj.blog.service.TagService;
import com.pcj.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


/**
 *
 * @className IndexController
 * @author pengcj
 * @date 2021/12/23 11:35
 */
@Controller
public class IndexController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/")
    public String index(@PageableDefault(size = 5,sort = "id",direction = Sort.Direction.DESC) Pageable pageable, Model model){
        model.addAttribute("page",blogService.listBlog(pageable));
        model.addAttribute("types",typeService.listTypeTop(6)); //查看博客最多的分类
        model.addAttribute("tags",tagService.listTop(10)); //查看博客最多的标签
        model.addAttribute("blogs",blogService.listTop(7));//查看推荐里面最新更新的博客
        return "index";
    }
    @RequestMapping("/search")
    public String Search(@PageableDefault(size = 1,sort = "updateTime",direction = Sort.Direction.DESC) Pageable pageable,
                         @RequestParam(required = false) String query, Model model){
        model.addAttribute("page",blogService.listBlog("%"+query+"%", pageable));
        model.addAttribute("query",query);
        return "search";
    }
    @GetMapping("/blog/{id}")  //根据id查询   首页点击标题进行跳转
    public String blog(@PathVariable Long  id,Model model){
        model.addAttribute("blog",blogService.getAndConverd(id));
        return "blog";
    }

    /**
     *  底部footer返回最新的三篇博客
     * @param model
     * @return
     */
    @GetMapping("/footer/newblog")
    public String newblogs(Model model){
        model.addAttribute("newblogs",blogService.listTop(3));
        return "_fragments :: newblogList";
    }



}
